package com.company.cloud.product.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.log.annotation.SysLog;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.product.modules.api.response.ProductCategoryModel;
import com.company.cloud.product.modules.base.entity.CategoryInfo;
import com.company.cloud.product.modules.base.entity.ProductInfo;
import com.company.cloud.product.modules.base.service.CategoryInfoService;
import com.company.cloud.product.modules.base.service.ProductInfoService;
import com.company.cloud.product.modules.cache.service.ProductConfigCacheService;
import com.company.cloud.product.modules.cache.utils.ProductCacheKey;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品分类服务
 */
@RestController
@RequestMapping("/app/prd/category")
@Api(tags = "Product_商品分类服务")
public class ProductCategoryController {

    @Autowired
    private ProductConfigCacheService productCacheService;

    @Autowired
    private CategoryInfoService categoryInfoService;

    @GetMapping("/tree")
    @SysLog("APP商品分类树")
    @ApiOperation(value = "APP商品分类树", response = BaseResult.class)
    public BaseResult query(@RequestParam(value = "type", defaultValue = "0") Integer type) {
        String companyNo = AuthContextUtils.getCompanyNo();
        List<ProductCategoryModel> data = productCacheService.getProductCategorysByCache(companyNo);
        List<ProductCategoryModel> result = new ArrayList<ProductCategoryModel>();
        for (ProductCategoryModel model : data) {
            if (type == -1) {
                if (model.getParentId() == 0) {
                    model.setChildren(this.getChildren(data, model.getId()));
                    result.add(model);
                }
            } else {
                if (model.getParentId() == 0 && hasProduct(model.getId())) {
                    model.setChildren(this.getChildren(data, model.getId()));
                    result.add(model);
                }
            }
        }
        /**
         * ！！！此处循环更新所有商品的treePath 和 分类的图。 谨慎打开
         */
       /* List<ProductInfo> productInfoList = productInfoService.list();
        for (ProductInfo productInfo : productInfoList) {
            Integer categoryId = productInfo.getCategoryId();
            if (categoryId != null) {
                CategoryInfo category = categoryInfoService.getById(categoryId);
                if (category != null && StringUtils.isEmpty(category.getImg())) {
                    category.setImg(productInfo.getImage());
                    categoryInfoService.updateById(category);
                }
                if (category != null && category.getTreePath() != null) {
                    productInfo.setTreePath(category.getTreePath());
                    productInfoService.updateById(productInfo);
                }
            }
        }*/

        return BaseResult.ok(result);
    }

    private List<ProductCategoryModel> getChildren(List<ProductCategoryModel> data, int parentId) {
        List<ProductCategoryModel> returnList = new ArrayList<ProductCategoryModel>();
        for (ProductCategoryModel model : data) {
            if (model.getParentId() == parentId && hasProduct(model.getId())) {
                model.setChildren(this.getChildren(data, model.getId()));
                returnList.add(model);
            }
        }
        return returnList;
    }

    private Boolean hasProduct(Integer categoryId) {
        int count = productCacheService.countProductCategoryByCache(categoryId);
        return count > 0;
    }

    @GetMapping("/get")
    @SysLog("商品分类表详情")
    @ApiOperation(value = "商品分类表详情查询")
    public BaseResult get(@RequestParam("id") Integer id) {
        ProductCategoryModel data = productCacheService.getProductCategoryByIdByDB(id);
        return BaseResult.ok(data);
    }

}
